package net.wanho.personalOffice.controller;

import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.servlet.BaseServlet;
import net.wanho.personalOffice.po.IndividualTask;
import net.wanho.personalOffice.service.IndividualTaskService;
import net.wanho.systemSettings.po.Employee;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/IndividualTaskServlet")
public class IndividualTaskServlet extends BaseServlet<IndividualTaskService> {
    /*
    查找
     */
    protected String list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String employeeId = request.getParameter("employeeId");
            if(StringUtils.isNotEmpty(employeeId)){
                return "/view/personalOffice/task/index.jsp";
            }
            List IndividualTaskList = super.baseService.findAll();
            request.setAttribute("IndividualTaskList",IndividualTaskList);
            return "/view/personalOffice/task/index.jsp";
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }

    protected String view(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String taskId = request.getParameter("taskId");
            IndividualTask individualTask = super.baseService.findById(taskId);
            request.setAttribute("individualTask",individualTask);
            return "/view/personalOffice/task/view.jsp";
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }
    protected String del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String taskId = request.getParameter("taskId");
            super.baseService.remove(taskId);
            return "redirect:/IndividualTaskServlet?method=list";
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }
    protected String upt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            IndividualTask individualTask = super.parseRequest(request, IndividualTask.class);
            super.baseService.modify(individualTask);
            return "redirect:/IndividualTaskServlet?method=list";
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }
    protected String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Integer relationsUser = Integer.parseInt(request.getParameter("relationsUser"));
            Integer responsiblePersonId = Integer.parseInt(request.getParameter("responsiblePersonId"));
            IndividualTask individualTask = super.parseRequest(request, IndividualTask.class);
            individualTask.getResponsiblePerson().setEmployeeId(responsiblePersonId);
            Employee employee = new Employee();
            employee.setEmployeeId(relationsUser);
            Date now = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            dateFormat.format(now);
            Date endTime = individualTask.getEndTime();
            dateFormat.format(endTime);
            individualTask.setEndTime(endTime);
            individualTask.setCreateTime(now);
            individualTask.getRelationsUsers().add(employee);
            boolean save = super.baseService.save(individualTask);
            return "redirect:/IndividualTaskServlet?method=list";
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }
    protected String addView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            List<Employee> employeeList = super.baseService.findEmployee();
            request.setAttribute("employeeList",employeeList);
            return "/view/personalOffice/task/create.jsp";
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }
}
